class Solution(object):
    def maximumSubsequenceCount(self, text, pattern):
        if pattern[0] == pattern[1]:
            num = text.count(pattern[0])
            return num * (num + 1) // 2
        else:
            n = len(text)
            pre_a = [0] * n
            pre_b = [0] * n
            if text[0] == pattern[0]:
                pre_a[0] = 1
            if text[0] == pattern[1]:
                pre_b[0] = 1
            for i in range(1, n):
                pre_a[i] = pre_a[i - 1]
                pre_b[i] = pre_b[i - 1]
                if text[i] == pattern[0]:
                    pre_a[i] += 1
                if text[i] == pattern[1]:
                    pre_b[i] += 1
            ans = 0
            for i in range(n):
                if text[i] == pattern[0]:
                    ans += pre_b[-1] - pre_b[i]
            ans += max(pre_a[-1], pre_b[-1])
            return ans